package ar.edu.unicen.exa.isistan.archassis;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Properties;

import ar.edu.unicen.exa.isistan.archassis.actions.IAction;
import ar.edu.unicen.exa.isistan.archassis.diff.LastVersionDiffStrategy;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.seed.ISeed;
import ar.edu.unicen.exa.isistan.archassis.rcs.IVersion;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNRevisionControlSystem;
import ar.edu.unicen.exa.isistan.archassis.rcs.svn.SVNVersion;
import ar.edu.unicen.exa.isistan.archassis.source.ISourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.source.RCSSourceStrategy;
import ar.edu.unicen.exa.isistan.archassis.util.IActionPrint;
import ar.edu.unicen.exa.isistan.archassis.util.MiscUtils;

public class TestSVN {
	private static void printSystemInfo() {
		for(String key :System.getenv().keySet()) {
			String value = System.getenv(key);
			System.out.println("System.getenv() " + key + ": " + value);
		}
		Properties properties = System.getProperties();
		for (Object key : properties.keySet()) {
			Object value = properties.get(key);
			System.out.println("System.getProperties() " + key + ": " + value);
		}
	}


	
	private static List<JavaClass> testSVN(String svnurl, IVersion lastVersion0, IVersion lastVersion1) {
		try {
			
			SVNRevisionControlSystem s = new SVNRevisionControlSystem(svnurl);
			
		
			DiffRequestData tempFile = s.getDiffRequestData();
			
			ISourceStrategy ss = new RCSSourceStrategy(s, lastVersion0, lastVersion1);
			
			return ss.getJavaClassList(new LastVersionDiffStrategy());
			
//			List<RCSFileDiff> listFD = s.getDiff(lastVersion0, lastVersion1);
//			for (RCSFileDiff f : listFD) {
//				System.out.println(f.toString());
//			}
//			
//			List<RCSFileDiff> listFD0 = Filter.filtrar(RCSFileModification.ADDED, listFD);
//			s.checkOut(lastVersion0, listFD0, tempFile);
//			
//			List<RCSFileDiff> listFD1 = Filter.filtrar(RCSFileModification.DELETED, listFD);
//			s.checkOut(lastVersion1, listFD1, tempFile);
			
			//return JavaDiffFacade.compare(tempFile, lastVersion0, lastVersion1);
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
		
	}
	
	private static List<JavaClass> testSVN(String svnurl, long r0, long r1) {
		IVersion lastVersion0 = new SVNVersion(r0, null);
		IVersion lastVersion1 = new SVNVersion(r1, null);
		return testSVN(svnurl, lastVersion0, lastVersion1);
	}
	
//	private static List<IAction> testSVN(DiffRequestData tempFile) {
//		try {
//			SVNRevisionControlSystem s = new SVNRevisionControlSystem(tempFile.getRepositoryURL());
//			IVersion lastVersion0 = null;
//			IVersion lastVersion1 = null;
//			List<IVersion> listV = s.getVersions();
//			for (IVersion v : listV) {
//				System.out.println(v.toString());
//				lastVersion0 = lastVersion1;
//				lastVersion1 = v;
//			}
//			return testSVN(tempFile, lastVersion0, lastVersion1);
//
//	
//		} catch (Exception e) {
//			e.printStackTrace();
//		} 
//		return null;
//		
//	}
		
//	public static void runDiff(String svnurl, String srcurl) {
//		DiffRequestData tempFile = new DiffRequestData(
//				svnurl,
//				srcurl
//			);
//		List<IAction> list = testSVN(tempFile);
//		System.out.println(getActionListText(list));
//	}
	
	public static void runDiff(String svnurl, long r0, long r1, boolean add) {
		
		DiffRequestData tempFile = new DiffRequestData(
				svnurl
			);
		List<JavaClass> list = testSVN(svnurl, r0, r1);

		//System.out.println(AllActionPrint.listToString(ClassResolver.getInstance().getSrcFolders()));
		
//		printToFile(list, tempFile, "", r0, r1, new NewClassEclipseActionPrint());
//
//		printToFile(list, tempFile, "", r0, r1, new NewClassActionPrint());
			
			//printToFile(list, tempFile, srcurl, r0, r1, new AllActionPrint());
			
		
		//List<JavaClass> la = NewClassEclipseActionPrint.filtrar(list);
		if (add) {
			//AIModuleFacade.getInstance().add(srcurl, la);
			return;
		} 
			
//		List<ListCasesResult> evalResultList = AIModuleFacade.getInstance().evaluate(list);
//		for (ListCasesResult evalResultItem: evalResultList) {
//			System.out.println(" === CASO [" + evalResultItem.getName() + "] " + evalResultItem.getPorcentajeCumplido());
//			
//			printCaso(evalResultItem);
//			
//			System.out.println("---------!!--------");
//		}
		
	}
	
	private static String IDENT = "--";
	private static void printSeed(String ident, ISeed seed, JavaClass javaClass) {
//		if ((seed instanceof RelacionUnariaSimple)) {
//			RelacionUnariaSimple rus = (RelacionUnariaSimple)seed;
//			System.out.println(rus.explain(javaClass));
//		} else if (!(seed instanceof ListSeed)) {
//			
//			float evalResult = -1;
//			if (javaClass != null) {
//				evalResult = seed.eval(javaClass);
//			}
//			System.out.println(ident + seed.toString() + " " + evalResult);
//		} else {
//			ListSeed compuesta = (ListSeed)seed;
//			System.out.println(ident + compuesta.toString());
//			for (ISeed s : compuesta.getListaSemillas()) {
//				printSeed(ident + IDENT, s, javaClass);
//			}
//			
//		}
//		
	}

	private static void printSeed(String ident, ISeed seed) {
//		if (!(seed instanceof ListSeed)) {
//			
//			float evalResult = -1;
//			System.out.println(ident + seed.toString() + " " + evalResult);
//		} else {
//			ListSeed compuesta = (ListSeed)seed;
//			System.out.println(ident + compuesta.toString());
//			for (ISeed s : compuesta.getListaSemillas()) {
//				printSeed(ident + IDENT, s);
//			}
//			
//		}
		
	}

	private static void printToFile(List<IAction> list, DiffRequestData tempFile, String srcurl, long r0, long r1, IActionPrint ap) {
		String sourseF = MiscUtils.plainFileName(srcurl);
		String actionString = ap.toString(list);
		
		File output = new File(tempFile.getRootTempPath(), sourseF+ "_" +r0 + "-" + r1 + "_" + ap.getClass().getSimpleName() + ".txt");
		MiscUtils.writeFile(actionString, output);
		
	}
	private static void setOutput(String file) {
		try {
			FileOutputStream fos = new FileOutputStream(new File(System.getProperties().get("user.dir") + "/tmp/" + file));
			PrintStream ps = new PrintStream(fos);
			System.setOut(ps);
		}catch(Exception e) {}
	}
	
	public static void main(String[] args) {
		
		//printSystemInfo();

		long rTo = 108;
		runDiff("http://svn.codespot.com/a/eclipselabs.org/svn-human-diff/trunk/", 60, rTo, true);
		//runDiff("http://svn.codespot.com/a/eclipselabs.org/svn-human-diff/trunk/", 60, rTo, false);
		

		//runDiff("http://svn.codespot.com/a/eclipselabs.org/logviewer/trunk/", 1, 60, false);
		
		

		runDiff("http://svn.codespot.com/a/eclipselabs.org/featureexplorer/trunk/", 1, 8, false);
		
//		runDiff("http://svn.codespot.com/a/eclipselabs.org/enotifier/trunk/", 1, 11);
		
		
		//runDiff("http://svn.codespot.com/a/eclipselabs.org/foos-bar/trunk/", 1, 120, false);
//		runDiff("http://svn.codespot.com/a/eclipselabs.org/navigatorext/trunk/", 1, 9);
		
		//runDiff("https://eclipsejsonedit.svn.sourceforge.net/svnroot/eclipsejsonedit/trunk", 1, 47);
//		runDiff("http://svn.codespot.com/a/eclipselabs.org/simtl/trunk/", 1, 8);
		
//		runDiff("http://svn.codespot.com/a/eclipselabs.org/graphics-procedural-modeling/trunk/", 1, 15);
		
//		runDiff("http://svn.codespot.com/a/eclipselabs.org/cppcheclipse/trunk/", 1, 224);
		
	}
}